/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socafeclient;


import entity.Recetas;
import entity.Recetasinsumos;
import entity.Insumos;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author wakowa
 */
public class TableModelRecetasInsumos extends AbstractTableModel{
    
    private ArrayList datalist = new ArrayList();
    private String[] columns = {"Seleccionar", "idInsumo", "Nombre","TipoDeMedida"};
    
    
    @Override
    public boolean isCellEditable(int row, int col)            
    {   
            return true;
    }
    
    @Override    
    public int getRowCount() {
        return datalist.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public Object getValueAt(int row, int col) {
       Object[] o = (Object[]) datalist.get(row);
       return o[col];
    }
    
    @Override
    public Class getColumnClass(int col) {
        switch (col) {
            case 0: //seleccionar
                return Boolean.class;
            case 1: //idInsumo
                return Integer.class;
            case 2: //Nombre
                return String.class;
            case 3: //TipoMedida
                return String.class;
            default:
                return null;
        }
    }
    
    public ArrayList getDataList()
    {
        return datalist;
    }
    
    public void setValueAt(int row, int col){
        Object[] o = (Object[]) datalist.get(row);
        o[0] = (o[0] == Boolean.TRUE) ? Boolean.FALSE : Boolean.TRUE;
    } 

    public TableModelRecetasInsumos(){}
    
    public void addTableModelRecetasInsumos(List<Insumos> insumos, List<Recetasinsumos> insumosEnLaReceta) {
        datalist.clear();
        for( Iterator<Insumos> i = insumos.iterator() ; i.hasNext();)
        {
           Insumos insumoDeLaLista = ((Insumos)i.next());
           Object[] o;
            Boolean checarCheckBox = false;
           for( Iterator<Recetasinsumos> iEnReceta = insumosEnLaReceta.iterator(); iEnReceta.hasNext();)
            {
                Insumos insumosDeLaReceta =  ((Recetasinsumos)iEnReceta.next()).getInsumos();
                checarCheckBox = insumosDeLaReceta.getIdinsumo().equals(insumoDeLaLista.getIdinsumo());               
            }
                o = new Object[]{checarCheckBox, insumoDeLaLista.getIdinsumo(),insumoDeLaLista.getNombre(),insumoDeLaLista.getTipomedida()};
               datalist.add(o);
        }
         fireTableDataChanged();
    }    
    
}
